#!/usr/bin/php
<?php
require_once('libs/pvc_project_generator.php');
require_once('libs/pvc_controller_generator.php');

$verbose = false;

$shortopts = "";
$shortopts .= "vh"; // v=verbose, h=help

$longopts = "";

$opts = getopt($shortopts);
shift($opts);
foreach(array_keys($opts) as $opt){ 
		//first check for the help option
		if($opt=="h"){
			print_help_message();
			exit(1);
		}

		if($opt=="v"){
			$verbose = true;
		}
}

if(isset($argv[1])){
	if($argv[1] == "create"){
		// Make sure we have a name also
	  if(!isset($argv[2])){
			echo("  => please enter a name for your project\n");
		} else {
			$prj = new PVC_Project_Generator($argv[2], $verbose);
		}
	}

	if($argv[1] == "generate") {
		if($argv[2] == "controller"){
			// make sure we have at least the name
			// of the controller to create
			if(!isset($argv[3])){
					echo("  => please enter a name for the controller\n");
			} else {
					$ctl = new PVC_Controller_Generator($argv[3], null, $verbose );
					if($ctl->is_correct_directory()){
						$ctl->generate();
					}
			}
		
		}
	}
} else {
	echo("\nPlease specify a subcommand\n");
	print_help_message();
	exit(1);
}


function print_help_message(){
				echo("\nhelp message\n");
}

function remove_element($arr, $val){
				foreach ($arr as $key => $value){
								if ($arr[$key] == $val){
												unset($arr[$key]);
								}
				}
				return $arr = array_values($arr);
}

function shift($options_array)
{
				foreach( $options_array as $o => $a )
				{
								// Look for all occurrences of option in argv and remove if found :
								// ----------------------------------------------------------------
								// Look for occurrences of -o (simple option with no value) or -o<val> (no space in between):
								while($k=array_search("-".$o.$a,$GLOBALS['argv']))
								{    // If found remove from argv:
												if($k)
																unset($GLOBALS['argv'][$k]);
								}
								// Look for remaining occurrences of -o <val> (space in between):
								while($k=array_search("-".$o,$GLOBALS['argv']))
								{    // If found remove both option and value from argv:
												if($k)
												{    unset($GLOBALS['argv'][$k]);
																unset($GLOBALS['argv'][$k+1]);
												}
								}
				}
				// Reindex :
				$GLOBALS['argv']=array_merge($GLOBALS['argv']);
}


?>
